package admin

import (
	"duoke/service"
	"duoke/tables"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"duoke/utils/valid"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

var CourseMenu = new(courseMenuCtl)

type courseMenuCtl struct{}

func (ctl *courseMenuCtl) List(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	list, err := service.CourseMenu.List(c, id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", list)
	return
}
func (ctl *courseMenuCtl) Save(c *gin.Context) {
	var req tables.CourseMenu
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	err := service.CourseMenu.Save(c, &req)
	if err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	response.Success(c, "保存成功", nil)
	return
}
func (ctl *courseMenuCtl) Del(c *gin.Context) {
	req := request.JsonMap(c)
	id := gconv.Int(req["id"])
	if id == 0 {
		response.Error(c, "参数缺失【id】", nil)
		return
	}
	err := service.CourseMenu.Del(c, id)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "删除成功", nil)
	return
}
func (ctl *courseMenuCtl) WxMenu(c *gin.Context) {
	menu, err := service.CourseMenu.WxMenu(c)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", menu)
	return
}
